package com.fz.controller;

import com.alibaba.fastjson.JSON;
import com.fz.entity.Shopaddress;
import com.fz.mapper.ShopaddressMapper;
import com.fz.util.Dbutil;
import com.fz.util.TestJava;
import org.apache.ibatis.session.SqlSession;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by webrx on 2017/7/1.
 */
@Controller
public class ShopingAddress {


    @RequestMapping(value = "/queryaddress",produces = {"application/json;charset=utf-8"}) @ResponseBody
    public String queryAdd() throws IOException {
        Dbutil db = new Dbutil();
        SqlSession ss =db.getSession();
        ShopaddressMapper address= ss.getMapper(ShopaddressMapper.class);
        List<Shopaddress> list = address.query();
        return JSON.toJSONString(list);
    }
    @RequestMapping(value = "/addaddress" ,method = RequestMethod.POST,produces = {"application/json;charset=utf-8"})
    public String addaddress(HttpServletRequest req , HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        resp.setContentType("text/html;charset=utf-8");
        Dbutil db02 = new Dbutil();
        SqlSession ss =db02.getSession();
        ShopaddressMapper address= ss.getMapper(ShopaddressMapper.class);

        String province01=req.getParameter("province");
        String city=req.getParameter("city");
        String area=req.getParameter("area");
        TestJava mu = new TestJava();

        List<Map<String, Object>> list = mu.query("province","province","where provinceid="+province01,"","");
        String provin =list.get(0).get("province").toString();

        List<Map<String, Object>> list02 = mu.query("city","city","where cityid="+city,"","");
        String city02 =list02.get(0).get("city").toString();

        List<Map<String, Object>> list03 = mu.query("area","area","where areaid="+area,"","");
        String area02 =list03.get(0).get("area").toString();

        String jiedao=req.getParameter("jiedao");
        String all =provin+city02+area02+jiedao;

        String username=req.getParameter("username");
        String phone=req.getParameter("phone");

        System.out.println(jiedao);
        System.out.println(city02);
        System.out.println(all);

        Shopaddress add = new Shopaddress();
        add.setUseraccount("admin");
        add.setUsername(username);
        add.setUserphone(phone);
        add.setUseraddress(all);
        address.add02(add);
        ss.commit();

        return "redirect:shop/shoping_car.jsp";
    }
}
